package org.jeecg.modules.InveotoryCenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.InveotoryCenter.entity.chayiEntity;
import org.jeecg.modules.InveotoryCenter.entity.chukudanEntity;
import org.jeecg.modules.InveotoryCenter.mapper.ChayichuliMapper;
import org.jeecg.modules.InveotoryCenter.service.ChayichuliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Chayi")
public class ChayichuliController {

    @Autowired
    ChayichuliService service;

    @Autowired
    ChayichuliMapper mapper;
    /**
     * 差异处理列表查询
     *
     * @param pageSize
     * @param pageNo
     * @param code
     * @return
     */
    @GetMapping("/chayiList")
    public Result<?> chayiList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String code,
            String differentTypeId
    ) {
        Page<chayiEntity> page = new Page<chayiEntity>(pageNo, pageSize);
        IPage<chayiEntity> pageList = service.getchayiList(page, code, differentTypeId);
        return Result.ok(pageList);
    }

    /**
     * 查询所有差异类型,返回list
     */
    @GetMapping("/getAlldifferentType")
    public Result<?> getAlldifferentType() {

        List<Map<String, String>> mapList = service.getgetAlldifferentType();
        return Result.ok(mapList);
    }

    /**
     * 新增差异处理
     */
    @PostMapping("/addchayichuli")
    public Result<?> addchayichuli(
            @RequestBody chayiEntity entity
    ) {

        try {
            //查询这个批次老的库存
            String  oldkucun=mapper.getpicioldkucun(entity.getRemainingInventoryId());
            if(entity.getDealNumber()>Double.parseDouble(oldkucun)){
                return Result.ok("处理数禁止大于库存数");
            }
            int count = service.addchayichuli(entity);
            return Result.ok("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("操作失败");
        }

    }


    /**
     * 通过差异表id查询详情信息
     */
    @GetMapping("/getchayidetail")

    public Result<?> getchayidetail(
            @RequestParam(name = "id") String id
    ) {
        chayiEntity entity = service.getchayidetail(id);
        return Result.ok(entity);
    }


    /**
     * 审核通过差异处理
     */
    @PostMapping("/applychayichuli")
    public Result<?> applychayichuli(
            @RequestBody HashMap<String,String> map
            ){

         try{


             //先将库存下批次的库存量减一下
             int count=service.applychayichuli(map.get("dealNumber"),map.get("remainingInventoryId"),map.get("id") );

             return Result.ok("操作成功");
         }catch (Exception e){
             e.printStackTrace();
             return Result.ok("操作失败");
         }

    }


    /**
     * 新增差异类型
     */
    @PostMapping("/adddifferenType")
    public Result<?> adddifferenType(
            @RequestBody chayiEntity entity
    ){
        int count=service.adddifferenType(entity);

        return Result.ok("操作成功");
    }


}
